package connector;

import java.io.File;
import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.util.Properties;

public class MapConfig 
{
    private String mapViewerUrl;
    private String dataSourceName;
    private String mapTileLayerName;
    private double initX; 
    private double initY;
    private int    initSrid = 8307; 
    private long   initMapScale;
    private String geoCoderUrl;
    private String routerUrl;
    
    public MapConfig() {
    }

    
    public void setMapViewerUrl(String mapViewerUrl) {
        this.mapViewerUrl = mapViewerUrl;
    }

    public String getMapViewerUrl() {
        return mapViewerUrl;
    }

    public void setMapTileLayerName(String mapTileLayerName) {
        this.mapTileLayerName = mapTileLayerName;
    }

    public String getMapTileLayerName() {
        return mapTileLayerName;
    }
    
    public void setInitX(double initX) {
        this.initX = initX;
    }

    public double getInitX() {
        return initX;
    }

    public void setInitY(double initY) {
        this.initY = initY;
    }

    public double getInitY() {
        return initY;
    }

    public void setInitSrid(int initSrid) {
        this.initSrid = initSrid;
    }

    public int getInitSrid() {
        return initSrid;
    }
    

    public void setGeoCoderUrl(String geoCoderUrl) {
        this.geoCoderUrl = geoCoderUrl;
    }

    public String getGeoCoderUrl() {
        return geoCoderUrl;
    }

    public void setRouterUrl(String routerUrl) {
        this.routerUrl = routerUrl;
    }

    public String getRouterUrl() {
        return routerUrl;
    }
    
    /**
     * Loads the config file and returns a MapConfig instance that represents
     * the config file. 
     * The config file is a .properties file containing following properties:
     *   mapviewer_url = 
     *   datasource_name=
     *   tile_layer_name = 
     *   init_x = 
     *   init_y = 
     *   init_srid =
     *   init_scale =
     *   geocoder_url =
     *   router_url =
     * 
     * @param file  path to the config file
     * @return a MapConfig instance if loaded successfully; null otherwise.
     */
    public static MapConfig loadConfigFromFile(String file) 
    {
        FileInputStream fis = null;
        
        try{
            fis = new FileInputStream(new File(file));
        
            Properties p = new Properties();
            p.load(fis);
            return loadConfigFromProperties(p);            
        }catch(Exception e) {
            System.err.println("Exception loading config file: "+e.getMessage());
            return null;
        }finally {
            if(fis!=null)
                try{ fis.close(); }catch(Exception ex){}
        }
    }
    
    public static void saveConfigToFile(MapConfig config, String file) 
    {
        if(config==null)
            return;
        
        FileOutputStream fos = null;
        try{
            fos = new FileOutputStream(new File(file));
            Properties p = new Properties();
            p.put("mapviewer_url", config.getMapViewerUrl());
            p.put("datasource_name", config.getDataSourceName());
            p.put("tile_layer_name", config.getMapTileLayerName());
            p.put("init_x", config.getInitX());
            p.put("init_y", config.getInitY());
            p.put("init_srid", config.getInitSrid());
            p.put("init_scale", config.getInitMapScale());
            p.put("geocoder_url", config.getGeoCoderUrl());
            p.put("router_url", config.getRouterUrl());
            p.store(fos, "");
            
        }catch(Exception e) {
            System.err.println("Eception saving config file: "+e.getMessage());
        } finally {
            if(fos!=null)
                try{fos.close();}catch(Exception ex){}
        }
    }

    public static MapConfig loadConfigFromProperties(Properties p) 
    {
        MapConfig res = new MapConfig();
        
        res.setMapViewerUrl(p.getProperty("mapviewer_url"));
        res.setDataSourceName(p.getProperty("datasource_name"));
        res.setMapTileLayerName(p.getProperty("tile_layer_name"));
        res.setInitX(Double.parseDouble( p.getProperty("init_x")));
        res.setInitY(Double.parseDouble(p.getProperty("init_y")));
        res.setInitMapScale(Long.parseLong(p.getProperty("init_scale")));
        res.setInitSrid(Integer.parseInt(p.getProperty("init_srid")));
        res.setGeoCoderUrl(p.getProperty("geocoder_url"));
        res.setRouterUrl(p.getProperty("router_url"));
        
        return res;
    }
    
    public void setDataSourceName(String dataSourceName) {
        this.dataSourceName = dataSourceName;
    }

    public String getDataSourceName() {
        return dataSourceName;
    }


    public void setInitMapScale(long initMapScale) {
        this.initMapScale = initMapScale;
    }

    public long getInitMapScale() {
        return initMapScale;
    }
}
